﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Milvus.Client;
  

namespace RagSharpCore.Service;

public static class FieldDataExtension
{
	public static object? GetValueBy(this FieldData fieldData, string propName = "Data")
	{
		// 获取实际类型（如 FieldData<string>、FloatVectorFieldData 等）
		Type actualType = fieldData.GetType();

		// 查找名为 "Data" 的公共属性
		PropertyInfo? dataProperty = actualType.GetProperty(propName);
		if (dataProperty == null)
		{
			return null; // 无 Data 属性（理论上不会出现，除非自定义子类）
		}
		if (dataProperty != null)
		{
			// 获取 Data 属性的值
			var data = dataProperty.GetValue(fieldData);
			Console.WriteLine($"data:{data}");
			return data;
		}
		return null;

	}

	public static object? GetData(this FieldData fieldData)
	{
		switch (fieldData.DataType)
		{
			// 标量类型（对应 FieldData<T>）
			case MilvusDataType.Bool:
				return (fieldData as FieldData<bool>)?.Data;
			case MilvusDataType.Int8:
				return (fieldData as FieldData<sbyte>)?.Data;
			case MilvusDataType.Int16:
				return (fieldData as FieldData<short>)?.Data;
			case MilvusDataType.Int32:
				return (fieldData as FieldData<int>)?.Data;
			case MilvusDataType.Int64:
				return (fieldData as FieldData<long>)?.Data;
			case MilvusDataType.VarChar:
				return (fieldData as FieldData<string>)?.Data;
			case MilvusDataType.String:
				return (fieldData as FieldData<string>)?.Data;
			case MilvusDataType.Float:
				return (fieldData as FieldData<float>)?.Data;
			case MilvusDataType.Double:
				return (fieldData as FieldData<double>)?.Data;
			case MilvusDataType.Json:  
				return (fieldData as FieldData<string>)?.Data;


			// 向量类型
			case MilvusDataType.FloatVector:
				return (fieldData as FloatVectorFieldData)?.Data;
			case MilvusDataType.BinaryVector:
				return (fieldData as BinaryVectorFieldData)?.Data;

			// 数组类型（假设为 Int64 数组，可扩展其他类型）
			case MilvusDataType.Array:
				return (fieldData as  ArrayFieldData<long>)?.Data;

			// 其他类型（根据需求补充）
			default:
				return null;
		}
	}


	public static IList<T> CastTo<T>(this object? obj, MilvusDataType dataType)
	{
		//if (datas is IReadOnlyList<string> varList)
		//{
		//    res = varList.Cast<T>().ToList();
		//}
		//return res;


		//var data = fieldData.GetData();
		var datas = obj;
		var res = new List<T>();
		switch (dataType)
		{

			// 标量类型（对应 FieldData<T>）
			case MilvusDataType.Int8:
				if (datas is IReadOnlyList<sbyte> sbyteList) res = sbyteList.Cast<T>().ToList();
				return res;
			case MilvusDataType.Int16:
				if (datas is IReadOnlyList<short> shortList) res = shortList.Cast<T>().ToList();
				return res;
			case MilvusDataType.Int32:
				if (datas is IReadOnlyList<int> intList) res = intList.Cast<T>().ToList();
				return res;
			case MilvusDataType.Int64:
				if (datas is IReadOnlyList<long> longList) res = longList.Cast<T>().ToList();
				return res;

			case MilvusDataType.VarChar:
				if (datas is IReadOnlyList<string> varList) res = varList.Cast<T>().ToList();
				return res;
			case MilvusDataType.String:
				if (datas is IReadOnlyList<string> strList) res = strList.Cast<T>().ToList();
				return res;
			//return (fieldData as FieldData<string>)?.Data;
			case MilvusDataType.Float:
				if (datas is IReadOnlyList<float> fltList) res = fltList.Cast<T>().ToList();
				return res;
			case MilvusDataType.Double:
				if (datas is IReadOnlyList<double> doubleList) res = doubleList.Cast<T>().ToList();
				return res;

			case MilvusDataType.Bool:
				if (datas is IReadOnlyList<bool> boolList) res = boolList.Cast<T>().ToList();
				return res;

			case MilvusDataType.Json: 
				if (datas is IReadOnlyList<string> json1List) res = json1List.Cast<T>().ToList(); 
				return res;

			// 向量类型
			case MilvusDataType.FloatVector:
				if (datas is IReadOnlyList<ReadOnlyMemory<float>> fvtList) res = fvtList.Cast<T>().ToList();
				return res;
			case MilvusDataType.BinaryVector:
				if (datas is IReadOnlyList<ReadOnlyMemory<byte>> bvtList) res = bvtList.Cast<T>().ToList();
				return res;
			// 数组类型（假设为 Int64 数组，可扩展其他类型）
			case MilvusDataType.Array:
				if (datas is IReadOnlyList<ReadOnlyMemory<long>> arrList) res = arrList.Cast<T>().ToList();
				return res;

			// 其他类型（根据需求补充）
			default:
				return res;

		}

	}


}
